package kibahed.university.effizientealgorithmen.fileinteractions;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;

import javax.swing.JFileChooser;

public class IOInteractions
{

	public static File openFile(final String[] extension)
	{
        final JFileChooser chooser = new JFileChooser("Verzeichnis wahlen");
        chooser.setDialogType(JFileChooser.OPEN_DIALOG);
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        chooser.setFileFilter(new javax.swing.filechooser.FileFilter() 
        {

            @Override
            public boolean accept(File f) 
            {
            	if(f.isDirectory())
            	{
            		return true;
            	}
            	for(String s:extension)
            	{
	                if(f.getName().endsWith(s))
	                {
	                    return true;
	                }
            	}
                return false;
            }

            @Override
            public String getDescription() {
                //throw new UnsupportedOperationException("Not supported yet.");
            	String description = "";
            	for(String s:extension){
            		description+="*"+s+"; ";
            	}
                return description;
            }
        });
        final File file = new File("/home");

        chooser.setCurrentDirectory(file);

        chooser.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent e) {
                if (e.getPropertyName().equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)
                        || e.getPropertyName().equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY)) {
                    final File f = (File) e.getNewValue();
                }
            }
        });
        
        chooser.setVisible(true);
        final int result = chooser.showOpenDialog(null);

        if (result == JFileChooser.APPROVE_OPTION) {
            File inputVerzFile = chooser.getSelectedFile();
            
            chooser.setVisible(false);
            return inputVerzFile;
           
        }
        
        chooser.setVisible(false);
        return null;
	}
}
